<template>
  <div class="manage">
    <el-dialog
      :title="operateType === 'add' ? '新增用户' : '更新用户'"
      :visible.sync="isShow"
    >
      <common-form
        :formLabel="operateFormLabel"
        :form="operateForm"
        ref="form"
      ></common-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="isShow = false">取 消</el-button>
        <el-button type="primary" @click="confirm">确定</el-button>
      </div>
    </el-dialog>
    <div class="manage-header">
      <el-button type="primary" @click="addUser" size="primary"
        >+ 新增</el-button
      >
      <el-upload
        class="upload-demo"
        action="http://49.235.42.82:8888/api/pub/v2/house_loan/importExcel"
        :on-preview="handlePreview"
        :on-remove="handleRemove"
        :before-remove="beforeRemove"
        multiple
        :limit="1"
        :on-success="onSuccess"
        :on-error="OnError"
        :on-exceed="handleExceed"
        :file-list="fileList"
      >
        <el-button size="primary" type="success">批量导入</el-button>
      </el-upload>
      <common-form inline :formLabel="formLabel" :form="searchFrom">
        <el-button
          type="primary"
          @click="getList(searchFrom.keyword)"
          size="primary"
          >搜索</el-button
        >
      </common-form>
    </div>
    <common-table
      :tableData="tableData"
      :tableLabel="tableLabel"
      :config="config"
      @changePage="getList()"
      @edit="editUser"
      @del="delUser"
    ></common-table>
  </div>
</template>

<script>
import CommonForm from "@/components/CommonForm";
import CommonTable from "@/components/CommonTable";
import { houseLoanSelectByPage } from "@/api/getHouseLoanData.js";
import { houseLoanDeleteById } from "@/api/getHouseLoanData.js";
import { houseLoanInsert } from "@/api/getHouseLoanData.js";
import {houseLoanUpdate } from "@/api/getHouseLoanData.js";
import { findByNameOrKey } from "@/api/getHouseLoanData.js";

export default {
  components: {
    CommonForm,
    CommonTable,
  },
  data() {
    return {
      operateType: "add",
      isShow: false,
      tableData: [],
      tableLabel: [
        {
          prop: "name",
          label: "姓名",
          required: true,
        },
        {
          prop: "sex",
          label: "性别",
          required: true,
        },
        {
          prop: "nation",
          label: "民族",
          required: true,
        },
        {
          prop: "birthDate",
          label: "出生日期",
          required: true,
        },
        {
          prop: "cardNum",
          label: "身份证号",
          required: true,
        },
        {
          prop: "homeAddress",
          label: "户籍地址",
          required: true,
        },
        {
          prop: "tel",
          label: "联系方式",
          required: true,
        },
 
        {
          prop: "currentAddress",
          label: "现住址",
          required: true,
        },
        {
          prop: "coupleName",
          label: "配偶姓名",
          required: true,
        },
        {
          prop: "coupleSex",
          label: "配偶性别",
          required: true,
        },
        {
          prop: "coupleNation",
          label: "配偶民族",
          required: true,
        },
        {
          prop: "coupleBirthDate",
          label: "配偶出生年月",
          required: true,
        },
        {
          prop: "coupleCardNum",
          label: "配偶身份证号",
          required: true,
        },
        {
          prop: "coupleHomeAddress",
          label: "配偶户籍地址",
          required: true,
        },
        {
          prop: "weddingDate",
          label: "婚姻登记日期",
          required: true,
        },
        {
          prop: "contractDate",
          label: "合同签订日期",
          required: true,
        },
        {
          prop: "loanDate",
          label: "放款日期",
          required: true,
        },
        {
          prop: "payDay",
          label: "还款日",
          required: true,
        },
        {
          prop: "payWay",
          label: "还款方式",
          required: true,
        },
        {
          prop: "loanSum",
          label: "借款金额",
          required: true,
        },
        {
          prop: "loanGoal",
          label: "借款用途",
          required: true,
        },
        {
          prop: "loanTimeLimit",
          label: "借款期限（月）",
          required: true,
        },
        {
          prop: "loanRate",
          label: "借款利率",
          required: true,
        },
        {
          prop: "overRate",
          label: "逾期利率",
          required: true,
        },
        {
          prop: "deadlineDate",
          label: "截止日期",
          required: true,
        },
        {
          prop: "principal",
          label: "欠款本金",
          required: true,
        },

        {
          prop: "interest",
          label: "欠款利息",
          required: true,
        },
        {
          prop: "amount",
          label: "标的额",
          required: true,
        },
        {
          prop: "litigationFee",
          label: "诉讼费",
          required: true,
        },
        {
          prop: "securityFee",
          label: "保全费",
          required: true,
        },
        {
          prop: "declaredFee",
          label: "公告费",
          required: true,
        },
        {
          prop: "lawyerFee",
          label: "律师费",
          required: true,
        },
        {
          prop: "guarantee",
          label: "保证人",
          required: true,
        },
        {
          prop: "guaranteeNature",
          label: "保证性质",
          required: true,
        },
        {
          prop: "guaranteePeriod",
          label: "保证期间",
          required: true,
        },
        {
          prop: "mortgageWay",
          label: "抵押方式",
          required: true,
        },
        {
          prop: "mortgageLocation",
          label: "抵押房产座落",
          required: true,
        },

        {
          prop: "mortgageDate",
          label: "办理抵押日期",
          required: true,
        },
        {
          prop: "mortgageNum",
          label: "不动产权号",
          required: true,
        },
        {
          prop: "mortgageCardNum",
          label: "抵押证号",
          required: true,
        },

        {
          prop: "financialReportNo",
          label: "财保案号",
          required: true,
        },
        {
          prop: "lawsuitNum",
          label: "诉讼案号",
          required: true,
        },
        {
          prop: "executeNum",
          label: "执行案号",
          required: true,
        },
        {
          prop: "judgePrincipal",
          label: "判决本金",
          required: true,
        },
        {
          prop: "judgeInterest",
          label: "判决利息",
          required: true,
        },
        {
          prop: "courtLocation",
          label: "原告/法院区域",
          required: true,
        },
        {
          prop: "accuserAddress",
          label: "原告地址",
          required: true,
        },
        {
          prop: "accuserPeople",
          label: "负责人",
          required: true,
        },
        {
          prop: "companyName",
          label: "公司名称",
          required: true,
        },
        {
          prop: "companyAddress",
          label: "公司地址",
          required: true,
        },
        {
          prop: "legalPersonName",
          label: "法人姓名",
          required: true,
        },
        {
          prop: "legalPersonPosition",
          label: "法人职位",
          required: true,
        },
        {
          prop: "paperSignDate",
          label: "文书落款日期",
          required: true,
        },
        {
          prop: "loanStartDate",
          label: "借款开始日期",
          required: true,
        },
        {
          prop: "loanEndDate",
          label: "借款结束日期",
          required: true,
        },
      ],
      config: {
        page: 1,
        total: 30,
        loading: false,
      },
      operateForm: {
        id: "",
        name: "",
        sex: "",
        nation: "",
        birthDate: "",
        cardNum: "",
        homeAddress: "",
        tel: "",
        currentAddress: "",
        workAddress: "",
        coupleName: "",
        coupleSex: "",
        coupleNation: "",
        coupleBirthDate: "",
        coupleCardNum: "",
        coupleHomeAddress: "",
        weddingDate: "",
        contractDate: "",
        loanDate: "",
        payDay: "",
        payWay: "",
        loanSum: "",
        loanGoal: "",
        loanTimeLimit: "",
        loanRate: "",
        overRate: "",
        deadlineDate: "",
        principal: "",
        interest: "",
        amount: "",
        litigationFee: "",
        securityFee: "",
        declaredFee: "",
        lawyerFee: "",
        gurantee: "",
        guaranteeNature: "",
        guranteePeriod: "",
        mortgageWay: "",
        mortgageLocation: "",
        mortgageDate: "",
        mortgageNum: "",
        mortgageCardNum: "",
        financialReportNo: "",
        lawsuitNum: "",
        executeNum: "",
        judgePrincipal: "",
        judgeInterest: "",
        courtLocation: "",
        accuserAddress: "",
        accuserPeople: "",
        companyName: "",
        companyAddress: "",
        legalPersonName: "",
        legalPersonPosition: "",
        paperSignDate: "",
        loanStartDate: "",
        loanEndDate: "",
      },
      operateFormLabel: [
        {
          model: "name",
          label: "姓名",
        },
        {
          model: "sex",
          label: "性别",
        type: "select",
          opts: [
            {
              label: "男",
              value: 1,
            },
            {
              label: "女",
              value: 0,
            },
          ]
        },
        {
          model: "nation",
          label: "民族",
        },
        {
          model: "birthDate",
          label: "出生日期",
              type: "date"
        },
        {
          model: "cardNum",
          label: "身份证号",
        },
        {
          model: "homeAddress",
          label: "户籍地址",
        },
        {
          model: "tel",
          label: "联系方式",
        },

        {
          model: "currentAddress",
          label: "现住址",
        },
        {
          model: "coupleName",
          label: "配偶姓名",
        },
        {
          model: "coupleSex",
          label: "配偶性别",
           type: "select",
          opts: [
            {
              label: "男",
              value: 1,
            },
            {
              label: "女",
              value: 0,
            },
        ]
        },
        {
          model: "coupleNation",
          label: "配偶民族",
        },
        {
          model: "coupleBirthDate",
          label: "配偶出生年月",
           type: "date"
        },
        {
          model: "coupleCardNum",
          label: "配偶身份证号",
        },
        {
          model: "coupleHomeAddress",
          label: "配偶户籍地址",
        },
        {
          model: "weddingDate",
          label: "婚姻登记日期",
           type: "date"
        },
        {
          model: "contractDate",
          label: "合同签订日期",
           type: "date"
        },
        {
          model: "loanDate",
          label: "放款日期",
           type: "date"
        },
        {
          model: "payDay",
          label: "还款日",
        },
        {
          model: "payWay",
          label: "还款方式",
        },
        {
          model: "loanSum",
          label: "借款金额",
        },
        {
          model: "loanGoal",
          label: "借款用途",
        },
        {
          model: "loanTimeLimit",
          label: "借款期限（月）",
        },
        {
          model: "loanRate",
          label: "借款利率",
        },
        {
          model: "overRate",
          label: "逾期利率",
        },
        {
          model: "deadlineDate",
          label: "截止日期",
           type: "date"
        },
        {
          model: "principal",
          label: "欠款本金",
        },

        {
          model: "interest",
          label: "欠款利息",
        },
        {
          model: "amount",
          label: "标的额",
        },
        {
          model: "litigationFee",
          label: "诉讼费",
        },
        {
          model: "securityFee",
          label: "保全费",
        },
        {
          model: "declaredFee",
          label: "公告费",
        },
        {
          model: "lawyerFee",
          label: "律师费",
        },
        {
          model: "guarantee",
          label: "保证人",
        },
        {
          model: "guaranteeNature",
          label: "保证性质",
        },
        {
          model: "guaranteePeriod",
          label: "保证期间",
        },
        {
          model: "mortgageWay",
          label: "抵押方式",
        },
        {
          model: "mortgageLocation",
          label: "抵押房产座落",
        },

        {
          model: "mortgageDate",
          label: "办理抵押日期",
           type: "date"
        },
        {
          model: "mortgageNum",
          label: "不动产权号",
        },
        {
          model: "mortgageCardNum",
          label: "抵押证号",
        },

        {
          model: "financialReportNo",
          label: "财保案号",
        },
        {
          model: "lawsuitNum",
          label: "诉讼案号",
        },
        {
          model: "executeNum",
          label: "执行案号",
        },
        {
          model: "judgePrincipal",
          label: "判决本金",
        },
        {
          model: "judgeInterest",
          label: "判决利息",
        },
        {
          model: "courtLocation",
          label: "原告/法院区域",
        },
        {
          model: "accuserAddress",
          label: "原告地址",
        },
        {
          model: "accuserPeople",
          label: "负责人",
        },
        {
          model: "companyName",
          label: "公司名称",
        },
        {
          model: "companyAddress",
          label: "公司地址",
        },
        {
          model: "legalPersonName",
          label: "法人姓名",
        },
        {
          model: "legalPersonPosition",
          label: "法人职位",
        },
        {
          model: "paperSignDate",
          label: "文书落款日期",
           type: "date"
        },
        {
          model: "loanStartDate",
          label: "借款开始日期",
           type: "date"
        },
        {
          model: "loanEndDate",
          label: "借款结束日期",
           type: "date"
        },
      ],
      searchFrom: {
        keyword: "",
      },
      formLabel: [
        {
          model: "keyword",
          label: "",
        },
      ],
    };
  },
  methods: {
      onSuccess()
    {
          this.$message({
          message: '信息录入成功，请刷新页面',
          type: 'success'
        });

    },
    OnError()
    {
      this.$message({
          message: '信息录入失败，请检查日期格式,正确格式为2021/1/19',
          type: 'error'
        });

    },
    async getList(name) {
      this.config.loading = false;
      // 搜索时，页码需要设置为1，才能正确返回数据，因为数据是从第一页开始返回的
      name ? (this.config.page = 1) : "";
      if (this.searchFrom.keyword == "") {
        const result = await houseLoanSelectByPage(this.config.page);
        console.log(result);
        if (result.data.code == 0) {
          this.tableData = result.data.data.houseLoans;
          this.config.total = result.data.data.count;
          this.config.loading = false;
        }
      } else {
        console.log(name);
        const result = await findByNameOrKey(
          this.searchFrom.keyword,
          this.config.page
        );
        console.log(result);

        this.tableData = result.data.data.houseLoans;
        this.config.total = result.data.data.count;
        this.config.loading = false;
      }
    },
    async SelectByName(name) {
      name ? (this.config.page = 1) : "";
      console.log(name);
      const result = await findByNameOrKey(name, this.config.page);
      console.log(result);

      this.tableData = result.data.data.creditCards;
      this.config.total = result.data.data.count;
      this.config.loading = false;
    },
    addUser() {
      this.operateForm = {};
      this.operateType = "add";
      this.isShow = true;
    },
    editUser(row) {
      this.operateType = "edit";
      this.isShow = true;
      this.operateForm = row;
    },
    confirm() {
      if (this.operateType === "edit") {
        this.operateForm.sex === 1
          ? (this.operateForm.sex = "男")
          : (this.operateForm.sex = "女");
       houseLoanUpdate(
          //       id, party, lawsuit_num, execute_num, sex, birth_date,
          // nation, card_num, tel, home_address, current_address,
          // work_address, principal, interest, amount, litigation_fee,
          // declared_fee, lawyer_fee, credit_card, deadline_date, open_date,
          // last_consume_date, last_pay_date, start_over_date, judge_principal, judge_interest,
          // court_location, paper_sign_date, course

       this.operateForm.id,
        this.operateForm.name,
        this.operateForm.sex,
        this.operateForm.nation,
        this.operateForm.birthDate,
        this.operateForm.cardNum,
        this.operateForm.homeAddress,
        this.operateForm.tel,
        this.operateForm.currentAddress,
        this.operateForm.workAddress,
        this.operateForm.coupleName,
        this.operateForm.coupleSex,
        this.operateForm.coupleNation,
        this.operateForm.coupleBirthDate,
        this.operateForm.coupleCardNum,
        this.operateForm.coupleHomeAddress,
        this.operateForm.weddingDate,
        this.operateForm.contractDate,
        this.operateForm.loanDate,
        this.operateForm.payDay,
        this.operateForm.payWay,
        this.operateForm.loanSum,
        this.operateForm.loanGoal,
        this.operateForm.loanTimeLimit,
        this.operateForm.loanRate,
        this.operateForm.overRate,
        this.operateForm.deadlineDate,
        this.operateForm.principal,
        this.operateForm.interest,
        this.operateForm.amount,
        this.operateForm.litigationFee,
        this.operateForm.securityFee,
        this.operateForm.declaredFee,
        this.operateForm.lawyerFee,
        this.operateForm.gurantee,
        this.operateForm.guaranteeNature,
        this.operateForm.guranteePeriod,
        this.operateForm.mortgageWay,
        this.operateForm.mortgageLocation,
        this.operateForm.mortgageDate,
        this.operateForm.mortgageNum,
        this.operateForm.mortgageCardNum,
        this.operateForm.financialReportNo,
        this.operateForm.lawsuitNum,
        this.operateForm.executeNum,
        this.operateForm.judgePrincipal,
        this.operateForm.judgeInterest,
        this.operateForm.courtLocation,
        this.operateForm.accuserAddress,
        this.operateForm.accuserPeople,
        this.operateForm.companyName,
        this.operateForm.companyAddress,
        this.operateForm.legalPersonName,
        this.operateForm.legalPersonPosition,
        this.operateForm.paperSignDate,
        this.operateForm.loanStartDate,
        this.operateForm.loanEndDate
        ).then((res) => {
          (this.isShow = false),
            this.$message({
              type: "success",
              message: "编辑成功!",
            });
          this.getList();
        });
      } else {
        // this.$http.post('/api/user/add', this.operateForm).then(res => {
        console.log(this.operateForm);
        // name, sex, nature, address, card_num, lawyer_id
        this.operateForm.sex === 1
          ? (this.operateForm.sex = "男")
          : (this.operateForm.sex = "女");
        houseLoanInsert(
                this.operateForm.name,
        this.operateForm.sex,
        this.operateForm.nation,
        this.operateForm.birthDate,
        this.operateForm.cardNum,
        this.operateForm.homeAddress,
        this.operateForm.tel,
        this.operateForm.currentAddress,
        this.operateForm.workAddress,
        this.operateForm.coupleName,
        this.operateForm.coupleSex,
        this.operateForm.coupleNation,
        this.operateForm.coupleBirthDate,
        this.operateForm.coupleCardNum,
        this.operateForm.coupleHomeAddress,
        this.operateForm.weddingDate,
        this.operateForm.contractDate,
        this.operateForm.loanDate,
        this.operateForm.payDay,
        this.operateForm.payWay,
        this.operateForm.loanSum,
        this.operateForm.loanGoal,
        this.operateForm.loanTimeLimit,
        this.operateForm.loanRate,
        this.operateForm.overRate,
        this.operateForm.deadlineDate,
        this.operateForm.principal,
        this.operateForm.interest,
        this.operateForm.amount,
        this.operateForm.litigationFee,
        this.operateForm.securityFee,
        this.operateForm.declaredFee,
        this.operateForm.lawyerFee,
        this.operateForm.gurantee,
        this.operateForm.guaranteeNature,
        this.operateForm.guranteePeriod,
        this.operateForm.mortgageWay,
        this.operateForm.mortgageLocation,
        this.operateForm.mortgageDate,
        this.operateForm.mortgageNum,
        this.operateForm.mortgageCardNum,
        this.operateForm.financialReportNo,
        this.operateForm.lawsuitNum,
        this.operateForm.executeNum,
        this.operateForm.judgePrincipal,
        this.operateForm.judgeInterest,
        this.operateForm.courtLocation,
        this.operateForm.accuserAddress,
        this.operateForm.accuserPeople,
        this.operateForm.companyName,
        this.operateForm.companyAddress,
        this.operateForm.legalPersonName,
        this.operateForm.legalPersonPosition,
        this.operateForm.paperSignDate,
        this.operateForm.loanStartDate,
        this.operateForm.loanEndDate
        ).then((res) => {
          if (res.data.code === 0) {
            console.log("插入成功");
            this.$message({
              type: "success",
              message: "添加成功!",
            });
          } else {
            console.log("插入失败");
            this.$message({
              type: "error",
              message: "添加失败!",
            });
          }
          (this.isShow = false), this.getList();
        });
      }
    },

    delUser(row) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let id = row.id;
          // this.$http
          //   .get('/api/v1/pub/defendant_one/deleteById', {
          //     params: {
          //       id
          //     }
          //   })
          const result = houseLoanDeleteById(id).then((res) => {
            this.$message({
              type: "success",
              message: "删除成功!",
            });
            this.getList();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
  created() {
    this.getList();
  },
};
</script>


<style lang="scss" scoped>
.manage {
  height: 90%;
  padding-bottom: 20px;
  padding-top: 20px;
  overflow: hidden;
  &-header {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
  }
}
</style>